USE [GD2C2011]
GO
/****** Objeto:  StoredProcedure [GOOGLECODERS].[UsuariosGet]    Fecha de la secuencia de comandos: 10/10/2011 18:10:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Pacay, Ariel>
-- Create date: <25/09/11, 1:00am>
-- Description:	<Trae filtro de UsuarioList, 
--				LIKE del UserName, 
--				y/o Habilitados solamente;
--				O trae todos si no se especifica>
-- =============================================
CREATE PROCEDURE [GOOGLECODERS].[UsuariosGet]
	-- Add the parameters for the stored procedure here
	@UsernameLike varchar(254) = '', 
	@SoloHabilitados bit = 1,
	@QueryUnions varchar(2000) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	DECLARE @TablaAux Table(IdRol int NOT NULL)

	IF(@QueryUnions <> '')
	BEGIN
		INSERT INTO @TablaAux
		EXEC(@QueryUnions)
	END

    -- Insert statements for procedure here
	SELECT DISTINCT u.IdUser IdUsuario, Username, Password, Active Habilitado 
	FROM Usuarios u
	LEFT JOIN Usuario_x_Rol ur ON u.IdUser = ur.IdUser
	WHERE 
	(Username LIKE '%'+@UsernameLike+'%' OR @UsernameLike = '') AND
	(Active = @SoloHabilitados OR @SoloHabilitados = 0) AND
	(IdRol IN (SELECT IdRol FROM @TablaAux) OR @QueryUnions = '')
END

